﻿using System;
using System.Collections.Generic;

namespace HCMS.Extensions
{
    /// <summary>
    /// Url 扩展类
    /// </summary>
    public static class UrlExt
    {
        /// <summary>
        /// 图片路径 Domain
        /// </summary>
        public static string ImagesDomain = "http://localhost:5020/";

        #region 图片路径处理
        /// <summary>
        /// 图片路径处理
        /// </summary>
        /// <param name="filePath">图片原始路径</param>
        /// <returns></returns>
        public static string DomainImages(this string filePath)
        {
            if (filePath == null || string.IsNullOrWhiteSpace(filePath))
            {
                return "";
            }

            var result = "";
            // 多图处理
            if (filePath.Contains(","))
            {
                var pathArray = filePath.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var pathList = new List<string>();
                foreach (var item in pathArray)
                {
                    if (item.StartsWith(ImagesDomain))
                    {
                        pathList.Add(item);
                    }
                    else
                    {
                        pathList.Add($"{ImagesDomain}{item}");
                    }
                }
                result = string.Join(",", pathList);
            }
            else
            {
                if (!filePath.StartsWith(ImagesDomain))
                {
                    result = $"{ImagesDomain}{filePath}";
                }
            }

            return result;
        }
        #endregion
    }
}
